package com.cqupt.controller;

import com.cqupt.common.log.annotation.OperationLog;
import com.cqupt.pojo.ResponseMessage;
import com.cqupt.pojo.Role;
import com.cqupt.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 角色
 */
@CrossOrigin
@Controller
@RequestMapping("role")
@ResponseBody
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 新增用户
     * @param role
     * @return
     */
    @OperationLog(level = "信息", object = "角色", operationType = "新增")
    @RequestMapping("/add_role")
    public ResponseMessage addRole(@RequestBody Role role) {
        Boolean status = roleService.addRole(role);
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        /**
         * 日志操作
         */
        resMap.put("log_objectName",role.getRoleName());
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("已存在"+role.getRoleName()+"!");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 根据角色名字获取角色信息
     * @param role
     * @return
     */
    @RequestMapping("/get_role_by_role_name")
    public ResponseMessage getRoleByRoleName(@RequestBody Role role) {
        Role result = roleService.getRoleByRoleName(role.getRoleName());
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("role", result);
        }});
        return responseMessage;
    }

    /**
     * 分页获取角色列表
     * @param param
     * @return
     */
    @RequestMapping("/get_role_by_page_number")
    public ResponseMessage getRoleByPageNumber(@RequestBody Map<String,Object> param) {
        int pageNumber = Integer.parseInt(param.get("pageNumber").toString());
        int pageSize = Integer.parseInt(param.get("pageSize").toString());
        PageInfo<Role> pageInfo = roleService.getRoleByPageNumber(pageNumber, pageSize);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setStatus(true);
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("pageInfo", pageInfo);
        }});
        return responseMessage;
    }

    /**
     * 通过角色名字更新角色信息
     * @param role
     * @return
     */
    @OperationLog(level = "信息", object = "角色", operationType = "修改")
    @RequestMapping("/update_role_by_name")
    public ResponseMessage updateRoleByName(@RequestBody Role role) {
        Boolean status = roleService.updateRoleByName(role);
        ResponseMessage responseMessage = new ResponseMessage();
        Map<String, Object> resMap = new HashMap<>();
        /**
         * 日志操作
         */
        resMap.put("log_objectName",role.getRoleName());
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 通过角色名字删除角色
     * @param role
     * @return
     */
    @OperationLog(level = "信息", object = "角色", operationType = "删除")
    @RequestMapping("/delete_role_by_name")
    public ResponseMessage deleteRoleByName(@RequestBody Role role) {
        Boolean status = roleService.deleteRoleByName(role);
        ResponseMessage responseMessage = new ResponseMessage();
        /**
         * 日志操作
         */
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("log_objectName",role.getRoleName());
        if (status == true){
            resMap.put("log_description","");
        }else{
            responseMessage.setErrorCode("");
        }
        responseMessage.setStatus(status);
        responseMessage.setContent(resMap);
        return responseMessage;
    }

    /**
     * 获取角色信息模板
     * @return
     */
    @RequestMapping("/get_default_role_template")
    public ResponseMessage getDefaultRoleTemplate() {
        String template = roleService.getDefaultRoleTemplate();
        ResponseMessage responseMessage = new ResponseMessage();
        if(template == null) {
            responseMessage.setStatus(false);
        } else {
            responseMessage.setStatus(true);
        }
        responseMessage.setContent(new HashMap<String, Object>(){{
            put("template", template);
        }});
        return responseMessage;
    }
}
